﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MobilnyDostawca.BusinessModels;
using MobilnyDostawca.Repositories;
using MobilnyDostawca.ViewModels;

namespace MobilnyDostawca.Controllers
{
    public class AuthenticationController : Controller
    {
        //
        // GET: /Account/LogIn
        public ActionResult LogIn()
        {
            return View();
        }

        //
        // POST: /Account/LogIn
        [HttpPost]
        public ActionResult LogIn(Nadzorcy model)
        {
            if (model.IsValid) 
            {
                FormsAuthentication.SetAuthCookie(model.Login, false);
                HttpContext.Session.Add("User", Repository.GetSupervisorByLogin(model.Login));
                return RedirectToAction("Index", "Home");
            }
            return View();
        }

        //
        // GET: /Account/ChangePassword
        public ActionResult ChangePassword()
        {
            return View();
        }

        //
        // GET: /Account/LogOff
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        //
        // POST: /Account/ChangePassword
        [HttpPost]
        public ActionResult ChangePassword(NadzorcyViewModel model)
        {
            if (model.IsValid)
            {
                Repository.UpdateSupervisor(model);
                return RedirectToAction("Index", "Home");
            }
            return View();
        }
    }
}
